package kitread.ui.widgets;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.border.CompoundBorder;
import javax.swing.border.MatteBorder;

import kitread.model.FeedItem;
import kitread.util.TextExtractor;

public class FeedItemCellRenderer extends JPanel implements ListCellRenderer<FeedItem> {

	private static final long serialVersionUID = -205632261557515874L;
	private JLabel labelUrl;
	private JLabel icon;
	private JPanel panel_icon;
	private JPanel panel_text;
	private JTextField labelTitle;
	private JTextArea labelDescription;

	/**
	 * Create the panel.
	 */
	public FeedItemCellRenderer() {
		setSize(new Dimension(200, 130));
		setPreferredSize(new Dimension(243, 126));
		setBorder(new CompoundBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(206, 206, 206)), new MatteBorder(1, 0, 0, 0, (Color) new Color(228, 228, 228))));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{30, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		panel_icon = new JPanel();
		panel_icon.setBackground(new Color(214, 214, 214));
		GridBagConstraints gbc_panel_icon = new GridBagConstraints();
		gbc_panel_icon.fill = GridBagConstraints.BOTH;
		gbc_panel_icon.gridx = 0;
		gbc_panel_icon.gridy = 0;
		add(panel_icon, gbc_panel_icon);
		GridBagLayout gbl_panel_icon = new GridBagLayout();
		gbl_panel_icon.columnWidths = new int[]{16, 0};
		gbl_panel_icon.rowHeights = new int[]{16, 0};
		gbl_panel_icon.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_icon.rowWeights = new double[]{0.0, Double.MIN_VALUE};
		panel_icon.setLayout(gbl_panel_icon);
		
		icon = new JLabel("");
		icon.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_icon = new GridBagConstraints();
		gbc_icon.insets = new Insets(10, 0, 0, 0);
		gbc_icon.weighty = 1.0;
		gbc_icon.weightx = 1.0;
		gbc_icon.anchor = GridBagConstraints.NORTH;
		gbc_icon.gridx = 0;
		gbc_icon.gridy = 0;
		panel_icon.add(icon, gbc_icon);
		
		panel_text = new JPanel();
		panel_text.setBackground(new Color(222, 222, 222));
		GridBagConstraints gbc_panel_text = new GridBagConstraints();
		gbc_panel_text.fill = GridBagConstraints.BOTH;
		gbc_panel_text.gridx = 1;
		gbc_panel_text.gridy = 0;
		add(panel_text, gbc_panel_text);
		GridBagLayout gbl_panel_text = new GridBagLayout();
		gbl_panel_text.columnWidths = new int[]{33, 0};
		gbl_panel_text.rowHeights = new int[]{15, 0, 0, 0};
		gbl_panel_text.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_text.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel_text.setLayout(gbl_panel_text);
		
		labelTitle = new JTextField();
		labelTitle.setFont(new Font("Dialog", Font.BOLD, 12));
		labelTitle.setBorder(null);
		labelTitle.setOpaque(false);
		labelTitle.setEditable(false);
		GridBagConstraints gbc_labelTitle = new GridBagConstraints();
		gbc_labelTitle.insets = new Insets(5, 5, 5, 0);
		gbc_labelTitle.fill = GridBagConstraints.BOTH;
		gbc_labelTitle.gridx = 0;
		gbc_labelTitle.gridy = 0;
		panel_text.add(labelTitle, gbc_labelTitle);
		labelTitle.setColumns(10);
		
		labelUrl = new JLabel("http://www.corriere.it");
		labelUrl.setFont(new Font("Dialog", Font.PLAIN, 12));
		labelUrl.setForeground(new Color(104, 136, 173));
		GridBagConstraints gbc_labelUrl = new GridBagConstraints();
		gbc_labelUrl.weighty = 0.5;
		gbc_labelUrl.weightx = 1.0;
		gbc_labelUrl.anchor = GridBagConstraints.WEST;
		gbc_labelUrl.insets = new Insets(2, 5, 5, 0);
		gbc_labelUrl.gridx = 0;
		gbc_labelUrl.gridy = 1;
		panel_text.add(labelUrl, gbc_labelUrl);
		
		labelDescription = new JTextArea();
		labelDescription.setForeground(new Color(128, 126, 136));
		labelDescription.setWrapStyleWord(true);
		labelDescription.setOpaque(false);
		labelDescription.setLineWrap(true);
		labelDescription.setEditable(false);
		labelDescription.setBorder(null);
		GridBagConstraints gbc_labelDescription = new GridBagConstraints();
		gbc_labelDescription.insets = new Insets(5, 5, 5, 5);
		gbc_labelDescription.fill = GridBagConstraints.BOTH;
		gbc_labelDescription.gridx = 0;
		gbc_labelDescription.gridy = 2;
		panel_text.add(labelDescription, gbc_labelDescription);

	}

	@Override
	public Component getListCellRendererComponent(JList list, FeedItem value,
		      int index, boolean isSelected, boolean cellHasFocus) {
		icon.setIcon(new ImageIcon(value.getIcon()));
		labelTitle.setText(value.getTitle());
		labelUrl.setText(value.getUrl().getHost());
		labelDescription.setText(TextExtractor.instance().trimString(value.getDescription(), 100));
		if (isSelected) {
			panel_icon.setBackground(new Color(195, 195, 195));
			panel_text.setBackground(new Color(209, 209, 209));
		} else {
			panel_icon.setBackground(new Color(214, 214, 214));
			panel_text.setBackground(new Color(222, 222, 222));
		}
		revalidate();
		repaint();
		return this;
	}

}
